﻿(function () {
    'use strict';

    angular.module('app', ['ui.router', 'ui.bootstrap', 'app.controllers', 'app.services', 'SignalR', 'angular-loading-bar', 'ngAnimate'])
    .constant('$const', {
        'appName': 'AisinGioro.WebPortal',
        'appVersion': 'v1.0',
        'signalrHub': 'msgHub',
        'author': 'Felix',
        'last': '2018-03-09'
    })
    .run(runBlock)
    .config(routeBlock)
    .config(configBlock)
    .filter('trustHtml', filterBlock);

    /** @ngInject */
    function runBlock($state, $rootScope, Hub, $const, $location) {
        //redirect to the page of routing
        $rootScope.routeTo = function (v, params) {
            $state.go(v, params, { reload: v });
        };

        //Listen routing events
        $rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
            $rootScope._navMainSelected = toState.name == 'main';
            $rootScope._navMonitorSelected = toState.name == 'monitor';
        });

        $rootScope.comingSoon = function () {
            alert('Coming soon!');
        };

        //Prevent event bubbling
        $rootScope.stopBubbling = function ($event) {
            // On recent browsers, only $event.stopPropagation() is needed
            if ($event.stopPropagation) $event.stopPropagation();
            if ($event.preventDefault) $event.preventDefault();
            $event.cancelBubble = true;
            $event.returnValue = false;
        }

        $rootScope.signalrUrl = $location.$$protocol + '://' + $location.$$host + ':' + $location.$$port + '/signalr';  //eg. http://127.0.0.1:9000/signalr
        $rootScope.wwwUrl = $location.$$protocol + '://' + $location.$$host + ':' + $location.$$port + '/wwwroot';  //eg. http://127.0.0.1:9000/wwwroot
    }

    /** @ngInject */
    function routeBlock($stateProvider, $urlRouterProvider) {
        $stateProvider
          .state('main', {
              url: '/',
              templateUrl: 'templates/main.html',
              controller: 'MainController',
              controllerAs: 'main'
          })
          .state('monitor', {
              url: '/monitor',
              templateUrl: 'templates/monitor.html',
              controller: 'MonitorController',
              controllerAs: 'model'
          })
          .state('ping', {
              url: '/ping',
              params: { 'ip': null },
              templateUrl: 'templates/ping.html',
              controller: 'PingController',
              controllerAs: 'model'
          })
          .state('telnet', {
              url: '/telnet',
              templateUrl: 'templates/telnet.html',
              controller: 'TelnetController',
              controllerAs: 'model'
          });

        $urlRouterProvider.otherwise('/');
    }

    /** @ngInject */
    function configBlock($logProvider, cfpLoadingBarProvider) {
        // Enable log
        $logProvider.debugEnabled(true);

        cfpLoadingBarProvider.includeSpinner = true;
        cfpLoadingBarProvider.includeBar = true;
        //cfpLoadingBarProvider.spinnerTemplate = '<div><span class="fa fa-spinner">Loading...</div>';
        //cfpLoadingBarProvider.latencyThreshold = 500;
    }

    /** @ngInject */
    function filterBlock($sce) {
        return function (input) {
            return $sce.trustAsHtml(input);
        };
    }    

})();
